package com.design_pattern._03_behavioral._05_strategy;

import java.util.*;

public class TestDriver {

    public static void main(String[] args) {
        Person season = Person.getBuilder().weight(80).age(30).heigh(177).build();
        Person other = Person.getBuilder().weight(83).age(26).heigh(167).build();
        Person other2 = Person.getBuilder().weight(70).age(30).heigh(178).build();
        List<Person> personList = Arrays.asList(season, other, other2);
        List<Comparator<Person>> comparatorList = Arrays.asList(new AgeComparator(), new HeighComparator(), new WeightComparator());

        // 还可以从map中，通过key，获得相应的策略
        System.out.println("age sort:");
        Collections.sort(personList, getComparator(comparatorList, "Age"));
        personList.forEach(System.out::println);
        System.out.println("heigh sort:");
        Collections.sort(personList, getComparator(comparatorList, "Heigh"));
        personList.forEach(System.out::println);
        System.out.println("weight sort:");
        Collections.sort(personList, getComparator(comparatorList, "Weight"));
        personList.forEach(System.out::println);
    }

    private static Comparator<Person> getComparator(List<Comparator<Person>> comparatorList, String arg){
        for (Comparator<Person> comparator : comparatorList) {
            if (comparator.getClass().getSimpleName().contains(arg)){ // 也可以用class.forName直接找到策略
                return comparator;
            }
        }
        return comparatorList.get(0);
    }
}
